package services

import (
    "regexp"
    "github.com/go-playground/validator/v10"
)

var validate *validator.Validate

func init() {
    validate = validator.New()
}

type ValidationService struct{}

func NewValidationService() *ValidationService {
    return &ValidationService{}
}

func (s *ValidationService) Validate(i interface{}) error {
    return validate.Struct(i)
}

// 自定义验证规则
func validateNQN(fl validator.FieldLevel) bool {
    nqn := fl.Field().String()
    pattern := `^nqn\.([\w\-\.]+)(:[\w\-\.]+)*$`
    match, _ := regexp.MatchString(pattern, nqn)
    return match
}

// 注册验证规则
func (s *ValidationService) RegisterValidations() {
    validate.RegisterValidation("nqn", validateNQN)
}
